﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PJAA_Inventory_and_AR___Prototype
{
    public partial class Issue_Invoice_Button : Form
    {
        private SQLMaster master;
        private string clientID;
        List<object[]> invlistofClient = new List<object[]>();
        private decimal total;
        private decimal discount;
        private decimal net;
        private string invoice_no;

        public Issue_Invoice_Button(string ClientID)
        {

            InitializeComponent();


            //added this block to make the form transparent
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;

            numericUpDown_Disc.Enabled = false;

            master = new SQLMaster();
            this.clientID = ClientID;


            addFirstEmptyValue();            
            loadInventoryofClient();
            loadProductsCombobox();

            groupBoxVisibility(false);
        }

        private void loadInventoryofClient()
        {
            invlistofClient = master.getClientInv(clientID);
            foreach (object[] strArray in invlistofClient)
            {
                ListViewItem item = new ListViewItem(strArray[0].ToString());
                item.SubItems.Add(strArray[1].ToString());
                listView1.Items.Add(item);
            }
        }

         private void PopulateCombobox(ComboBox ComboBoxtoPopulate, List<object[]> arrayOfInventoryType)
        {
            foreach (object[] strArray in arrayOfInventoryType)
            {
                //for (int i = 0; i <= strArray.Length - 1; i++)
                //{
                    //string valueInsideArray = strArray[i].ToString();
                    ComboBoxtoPopulate.Items.Add(strArray[0]);//.ToString().Substring(6,valueInsideArray.Length-6));
                //}
            }
        }

        //loadpics combobox
        private void loadProductsCombobox()
        {
            invlistofClient= master.getClientInv(clientID);

            PopulateCombobox(Product_ComboBox1, invlistofClient);
            PopulateCombobox(Product_ComboBox2, invlistofClient);
            PopulateCombobox(Product_ComboBox3, invlistofClient);
            PopulateCombobox(Product_ComboBox4, invlistofClient);
            PopulateCombobox(Product_ComboBox5, invlistofClient);
            PopulateCombobox(Product_ComboBox6, invlistofClient);
            PopulateCombobox(Product_ComboBox7, invlistofClient);
            PopulateCombobox(Product_ComboBox8, invlistofClient);
            PopulateCombobox(Product_ComboBox9, invlistofClient);
            PopulateCombobox(Product_ComboBox10, invlistofClient);
        }



      
        private void linkLabel_more_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

            groupBox_ProductInv.Height += 25;
            int loc_y = Convert.ToInt16(linkLabel_more.Location.Y.ToString());
            linkLabel_more.Location = new Point(Convert.ToInt16(linkLabel_more.Location.X.ToString()), loc_y + 25);
        }
        
        #region computations/changes

        private void Product_ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox1.Text == "" || Product_ComboBox1.Text == "[Select Product]")
            {}
            else
            {
                txtBox_UnitPrice1.Text = master.getUnitcostofitemType(Product_ComboBox1.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty1_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox1.Text == "" || Product_ComboBox1.Text == "[Select Product]")
            {
                ProdQty1.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice1.Text) * ProdQty1.Value;
                txtBox_Gross1.Text = gross.ToString();
            }
        }

        private void txtBox_Gross1_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }

        private void Product_ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox2.Text == "" || Product_ComboBox2.Text == "[Select Product]")
            { }
            else
            {
                txtBox_UnitPrice2.Text = master.getUnitcostofitemType(Product_ComboBox2.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty2_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox2.Text == "" || Product_ComboBox2.Text == "[Select Product]")
            {
                ProdQty2.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice2.Text) * ProdQty2.Value;
                txtBox_Gross2.Text = gross.ToString();
            }
        }

        private void txtBox_Gross2_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }


        private void Product_ComboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox3.Text == "" || Product_ComboBox3.Text == "[Select Product]")
            { }
            else
            {
                txtBox_UnitPrice3.Text = master.getUnitcostofitemType(Product_ComboBox3.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty3_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox3.Text == "" || Product_ComboBox3.Text == "[Select Product]")
            {
                ProdQty3.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice3.Text) * ProdQty3.Value;
                txtBox_Gross3.Text = gross.ToString();
            }
        }

        private void txtBox_Gross3_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }

        private void Product_ComboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox4.Text == "" || Product_ComboBox4.Text == "[Select Product]")
            { }
            else
            {
                txtBox_UnitPrice4.Text = master.getUnitcostofitemType(Product_ComboBox4.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty4_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox4.Text == "" || Product_ComboBox4.Text == "[Select Product]")
            {
                ProdQty4.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice4.Text) * ProdQty4.Value;
                txtBox_Gross4.Text = gross.ToString();
            }
        }

        private void txtBox_Gross4_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }

        private void Product_ComboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox5.Text == "" || Product_ComboBox5.Text == "[Select Product]")
            { }
            else
            {
                txtBox_UnitPrice5.Text = master.getUnitcostofitemType(Product_ComboBox5.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty5_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox5.Text == "" || Product_ComboBox5.Text == "[Select Product]")
            {
                ProdQty5.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice5.Text) * ProdQty5.Value;
                txtBox_Gross5.Text = gross.ToString();
            }
        }

        private void txtBox_Gross5_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }
        private void Product_ComboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox6.Text == "" || Product_ComboBox6.Text == "[Select Product]")
            { }
            else
            {
                txtBox_UnitPrice6.Text = master.getUnitcostofitemType(Product_ComboBox6.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty6_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox6.Text == "" || Product_ComboBox6.Text == "[Select Product]")
            {
                ProdQty6.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice6.Text) * ProdQty6.Value;
                txtBox_Gross6.Text = gross.ToString();
            }
        }

        private void txtBox_Gross6_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }
        

        
        private void Product_ComboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox7.Text == "" || Product_ComboBox7.Text == "[Select Product]")
            { }
            else
            {
                txtBox_UnitPrice7.Text = master.getUnitcostofitemType(Product_ComboBox7.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty7_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox7.Text == "" || Product_ComboBox7.Text == "[Select Product]")
            {
                ProdQty7.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice7.Text) * ProdQty7.Value;
                txtBox_Gross7.Text = gross.ToString();
            }
        }

        private void txtBox_Gross7_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }

        private void Product_ComboBox8_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox8.Text == "" || Product_ComboBox8.Text == "[Select Product]")
            { }
            else
            {
                txtBox_UnitPrice8.Text = master.getUnitcostofitemType(Product_ComboBox8.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty8_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox8.Text == "" || Product_ComboBox8.Text == "[Select Product]")
            {
                ProdQty8.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice8.Text) * ProdQty8.Value;
                txtBox_Gross8.Text = gross.ToString();
            }
        }

        private void txtBox_Gross8_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }

        private void Product_ComboBox9_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox9.Text == "" || Product_ComboBox9.Text == "[Select Product]")
            { }
            else
            {
                txtBox_UnitPrice9.Text = master.getUnitcostofitemType(Product_ComboBox9.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty9_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox9.Text == "" || Product_ComboBox9.Text == "[Select Product]")
            {
                ProdQty9.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice9.Text) * ProdQty9.Value;
                txtBox_Gross9.Text = gross.ToString();
            }
        }

        private void txtBox_Gross9_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }

        private void Product_ComboBox10_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox10.Text == "" || Product_ComboBox10.Text == "[Select Product]")
            { }
            else
            {
                txtBox_UnitPrice10.Text = master.getUnitcostofitemType(Product_ComboBox10.Text.ToString().Substring(0, 3)).ToString();
            }
        }

        private void ProdQty10_ValueChanged(object sender, EventArgs e)
        {
            if (Product_ComboBox10.Text == "" || Product_ComboBox10.Text == "[Select Product]")
            {
                ProdQty10.Value = 0;
                MessageBox.Show("Please pick a product first", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                decimal gross = Convert.ToDecimal(txtBox_UnitPrice10.Text) * ProdQty10.Value;
                txtBox_Gross10.Text = gross.ToString();
            }
        }

        private void txtBox_Gross10_TextChanged(object sender, EventArgs e)
        {
            IssueInv_Inv_TotalXX_Lbl.Text = this.recomputeTotalNet();
        }

#endregion
        
        public string recomputeTotalNet()
        {
            total = 0;
            if (txtBox_Gross1.Text != "")
            { total += Convert.ToDecimal(txtBox_Gross1.Text); }
            if (txtBox_Gross2.Text != "")
            { total += Convert.ToDecimal(txtBox_Gross2.Text); }
            if (txtBox_Gross3.Text != "")
            { total += Convert.ToDecimal(txtBox_Gross3.Text); }
            if (txtBox_Gross4.Text != "")
            {total += Convert.ToDecimal(txtBox_Gross4.Text);}
            if (txtBox_Gross5.Text != "")
            {total += Convert.ToDecimal(txtBox_Gross5.Text);}
            if (txtBox_Gross6.Text != "")
            {total += Convert.ToDecimal(txtBox_Gross6.Text);}
            if (txtBox_Gross7.Text != "")
            {total += Convert.ToDecimal(txtBox_Gross7.Text);}
            if (txtBox_Gross8.Text != "")
            {total += Convert.ToDecimal(txtBox_Gross8.Text);}
            if (txtBox_Gross9.Text != "")
            {total += Convert.ToDecimal(txtBox_Gross9.Text);}
            if (txtBox_Gross10.Text != "")
            {total += Convert.ToDecimal(txtBox_Gross10.Text);}
            
            

            return total.ToString();
        }







        private void IssueInv_Inv_WDsct_RadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            if(IssueInv_Inv_WDsct_RadioBtn.Checked)
                numericUpDown_Disc.Enabled = true;
        }

        private void IssueInv_Inv_WODsct_RadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            if (IssueInv_Inv_WODsct_RadioBtn.Checked)
            {
                numericUpDown_Disc.Enabled = false;
                numericUpDown_Disc.Value= 0;
                label_net.Text = IssueInv_Inv_TotalXX_Lbl.Text;
            }

        }


        private void numericUpDown_Disc_ValueChanged(object sender, EventArgs e)
        {
            if (IssueInv_Inv_WDsct_RadioBtn.Checked)
            {
                if (IssueInv_Inv_TotalXX_Lbl.Text != "")
                {
                    discount= ((Convert.ToDecimal(IssueInv_Inv_TotalXX_Lbl.Text) * (numericUpDown_Disc.Value * 0.01m)));
                    net = (Convert.ToDecimal(IssueInv_Inv_TotalXX_Lbl.Text) - discount);
                    label_net.Text = net.ToString("0.00");
                }
            }

            else
            {
                label_net.Text = IssueInv_Inv_TotalXX_Lbl.Text;
            }

        }

        private void addFirstEmptyValue()
        {
            Product_ComboBox1.Items.Add("");
            Product_ComboBox2.Items.Add("");
            Product_ComboBox3.Items.Add("");
            Product_ComboBox4.Items.Add("");
            Product_ComboBox5.Items.Add("");
            Product_ComboBox6.Items.Add("");
            Product_ComboBox7.Items.Add("");
            Product_ComboBox8.Items.Add("");
            Product_ComboBox9.Items.Add("");
            Product_ComboBox10.Items.Add("");

        }

        private void button_okInvoice_Click(object sender, EventArgs e)
        {
            if (IssueInv_InvNo_txtBox.Text == "")
            {
                MessageBox.Show("Please input an invoice number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (master.checkInvoiceNumberduplication(IssueInv_InvNo_txtBox.Text) == true)
                {
                    MessageBox.Show("Please input a proper invoice number. \n Invoice number placed is a duplicate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    this.invoice_no = IssueInv_InvNo_txtBox.Text;
                    groupBoxVisibility(true);
                }
            }
        }

        private void groupBoxVisibility(bool boolVisib)
        {
            if (boolVisib == true)
            {
                IssueInv_Inv_Dsct_GrpBox.Visible = true;
                groupBox_ProductInv.Visible = true;
                groupBox1.Visible = true;
                linkLabel_more.Visible = true;
                button_ok.Visible = true;
                button_cancel.Visible = true;
            }

            else
            {
                IssueInv_Inv_Dsct_GrpBox.Visible = false;
                groupBox_ProductInv.Visible = false;
                groupBox1.Visible = false;
                linkLabel_more.Visible = false;
                button_ok.Visible = false;
                button_cancel.Visible =false;
            }
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            string year = DateTime.Now.ToString("yyyy");
            string month = DateTime.Now.ToString("MM");
            string day = DateTime.Now.ToString("dd");
            

            //add sales invoice
            //KAIALNGAN PALITAN ANG cONSIGNMENT
            if (IssueInv_Inv_TotalXX_Lbl.Text == "")
            {
                MessageBox.Show("Please enter products first before proceeding or click cancel", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {
                if (checkIfqtyInputIsValid() == true)
                {//add sales invoice
                    master.addSalesInvoice(invoice_no, this.clientID, year + "-" + month + "-" + day,
                       "Consignment", Convert.ToDecimal(IssueInv_Inv_TotalXX_Lbl.Text), this.discount, this.net);

                    createInvoiceLine(); //call method to add invoice lines

                    master.addInvoiceCompletion(this.invoice_no, 0);//add invoice completion

                    //show confirmation
                    MessageBox.Show("Succesfully issued an invoice", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else 
                { 

                }
            }
            
        }

        // caling this will add all combobox that has a product on it
        private void createInvoiceLine()
        {
            

            if (Product_ComboBox1.Text == "" || Product_ComboBox1.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv=master.getCurrentClientItemInventory(this.clientID, Product_ComboBox1.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty1.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox1.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty1.Value), Convert.ToDecimal(txtBox_UnitPrice1.Text), Convert.ToDecimal(txtBox_Gross1.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox1.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty1.Value));
                     
                }
            }

            if (Product_ComboBox2.Text == "" || Product_ComboBox2.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox2.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty2.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox2.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty2.Value), Convert.ToDecimal(txtBox_UnitPrice2.Text), Convert.ToDecimal(txtBox_Gross2.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox2.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty2.Value));
                     
                }
            }

            if (Product_ComboBox3.Text == "" || Product_ComboBox3.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox3.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty3.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox3.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty3.Value), Convert.ToDecimal(txtBox_UnitPrice3.Text), Convert.ToDecimal(txtBox_Gross3.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox3.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty3.Value));
                     
                }
            }

            if (Product_ComboBox4.Text == "" || Product_ComboBox4.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox4.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty4.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox4.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty4.Value), Convert.ToDecimal(txtBox_UnitPrice4.Text), Convert.ToDecimal(txtBox_Gross4.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox4.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty4.Value));
                     
                }
            }

            if (Product_ComboBox5.Text == "" || Product_ComboBox5.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox5.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty5.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox5.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty5.Value), Convert.ToDecimal(txtBox_UnitPrice5.Text), Convert.ToDecimal(txtBox_Gross5.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox5.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty5.Value));
                     
                }
            }

            if (Product_ComboBox6.Text == "" || Product_ComboBox6.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox6.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty6.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox6.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty6.Value), Convert.ToDecimal(txtBox_UnitPrice6.Text), Convert.ToDecimal(txtBox_Gross6.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox6.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty6.Value));
                     
                }
            }

            if (Product_ComboBox7.Text == "" || Product_ComboBox7.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox7.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty7.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox7.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty7.Value), Convert.ToDecimal(txtBox_UnitPrice7.Text), Convert.ToDecimal(txtBox_Gross7.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox7.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty7.Value));
                     
                }
            }

            if (Product_ComboBox8.Text == "" || Product_ComboBox8.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox8.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty8.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox8.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty8.Value), Convert.ToDecimal(txtBox_UnitPrice8.Text), Convert.ToDecimal(txtBox_Gross8.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox8.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty8.Value));
                     
                }
            }

            if (Product_ComboBox9.Text == "" || Product_ComboBox9.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox9.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty9.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox9.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty9.Value), Convert.ToDecimal(txtBox_UnitPrice9.Text), Convert.ToDecimal(txtBox_Gross9.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox9.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty9.Value));
                     
                }
            }

            if (Product_ComboBox10.Text == "" || Product_ComboBox10.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox10.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty10.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    master.addSalesInvoiceLine(invoice_no, Product_ComboBox10.Text.Substring(0, 3),
                        Convert.ToInt16(ProdQty10.Value), Convert.ToDecimal(txtBox_UnitPrice10.Text), Convert.ToDecimal(txtBox_Gross10.Text));
                    master.UpdateGoodsOnConsignment(this.clientID, Product_ComboBox10.Text.Substring(0, 3), currClientiv - Convert.ToInt16(ProdQty10.Value));
                     
                }
            }




            
        }

        private bool checkIfqtyInputIsValid()
        {
            bool allclear = false;

            if (Product_ComboBox1.Text == "" || Product_ComboBox1.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox1.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty1.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    allclear = true;
                }
            }

            if (Product_ComboBox2.Text == "" || Product_ComboBox2.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox2.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty2.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                 
                    allclear = true;
                }
            }

            if (Product_ComboBox3.Text == "" || Product_ComboBox3.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox3.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty3.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    allclear = true;
                }
            }

            if (Product_ComboBox4.Text == "" || Product_ComboBox4.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox4.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty4.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    allclear = true;
                }
            }

            if (Product_ComboBox5.Text == "" || Product_ComboBox5.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox5.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty5.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                 
                    allclear = true;
                }
            }

            if (Product_ComboBox6.Text == "" || Product_ComboBox6.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox6.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty6.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                 
                    allclear = true;
                }
            }

            if (Product_ComboBox7.Text == "" || Product_ComboBox7.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox7.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty7.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                 
                    allclear = true;
                }
            }

            if (Product_ComboBox8.Text == "" || Product_ComboBox8.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox8.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty8.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                 
                 
                    allclear = true;
                }
            }

            if (Product_ComboBox9.Text == "" || Product_ComboBox9.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox9.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty9.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    allclear = true;
                }
            }

            if (Product_ComboBox10.Text == "" || Product_ComboBox10.Text == "[Select Product]")
            { }
            else
            {
                //assign the current value of the item to a variable
                int currClientiv = master.getCurrentClientItemInventory(this.clientID, Product_ComboBox10.Text.Substring(0, 3));

                if (Convert.ToInt16(ProdQty10.Value) > currClientiv)
                {
                    MessageBox.Show("Quantity inputted for one of the items has a greater value than what the customer has", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    allclear = true;
                }
            }




            return allclear;
        }

        //method will not allow other inputs except numbers
        private void IssueInv_InvNo_txtBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
            {
                // Digits are OK
            }

            else if (e.KeyChar == '\b')
            {
                // Backspace key is OK      
            }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //    MessageBeep();
            }
        }

        private void Issue_Invoice_Button_Load(object sender, EventArgs e)
        {

        }

        private void radioButton_maunal_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_manual.Checked)
            {
                IssueInv_InvNo_txtBox.Clear();
                IssueInv_InvNo_txtBox.Enabled = true;
                IssueInv_InvNo_txtBox.ReadOnly = false;
                IssueInv_InvNo_txtBox.BackColor = System.Drawing.Color.White;
            }
            else { }

        }

        private void radioButton_auto_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_auto.Checked)
            {
                IssueInv_InvNo_txtBox.Clear();
                IssueInv_InvNo_txtBox.Enabled = false;
                IssueInv_InvNo_txtBox.ReadOnly = true;
                IssueInv_InvNo_txtBox.BackColor = System.Drawing.Color.LightGray;

                if (master.checkifNoAutomatedInvoiceNumberRecordedYet() == false)
                { //if there are no automated invoice number recorded yet, create a new entry
                    IssueInv_InvNo_txtBox.Text = "A00001";
                }
                else 
                {// there is a record for automated invoice number
                    decimal invoiceNumOnly=Convert.ToDecimal(master.getLastAutomatedInvoiceNumber().Substring(1,5));
                    invoiceNumOnly++;
                    IssueInv_InvNo_txtBox.Text = "A" + invoiceNumOnly.ToString().PadLeft(5, '0');
                }
            }
            else { }
        }

        private void IssueInv_Inv_TotalXX_Lbl_TextChanged(object sender, EventArgs e)
        {
            if (IssueInv_Inv_WODsct_RadioBtn.Checked)
            {
                label_net.Text = IssueInv_Inv_TotalXX_Lbl.Text;
            }
            else if (IssueInv_Inv_WDsct_RadioBtn.Checked)
            {
                discount = ((Convert.ToDecimal(IssueInv_Inv_TotalXX_Lbl.Text) * (numericUpDown_Disc.Value * 0.01m)));
                net = (Convert.ToDecimal(IssueInv_Inv_TotalXX_Lbl.Text) - discount);
                label_net.Text = net.ToString("0.00");
            }
        }



        
        



    }
}
